package com.qs.home.ui.VM;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.view.View;

import com.qs.home.BR;
import com.qs.home.R;
import com.qs.home.databinding.HomeSelectLanguageItemBinding;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

/**
 * 翻译语种选择vm
 *
 * @Author ltzz
 * @Date 2019/8/21
 */
public class SelectLanguageViewModel extends BaseViewModel {
    public SelectLanguageViewModel(@NonNull Application application) {
        super(application);
    }

    //当前选择的语言
    public ObservableField<String> selectLanguage = new ObservableField<>("");


    private SelectLanguageItemViewModel oldItem;
    private HomeSelectLanguageItemBinding oldBinding;
    //给RecyclerView添加ObservableList
    public ObservableList<SelectLanguageItemViewModel> observableList = new ObservableArrayList<>();
    //RecyclerView多布局添加ItemBinding
    public ItemBinding<SelectLanguageItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<SelectLanguageItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, SelectLanguageItemViewModel item) {
            //设置布局
            itemBinding.set(BR.viewModel, R.layout.home_select_language_item);
        }
    });
    //给RecyclerView添加Adpter，请使用自定义的Adapter继承BindingRecyclerViewAdapter，重写onBindBinding方法，里面有你要的Item对应的binding对象
    public BindingRecyclerViewAdapter<SelectLanguageItemViewModel> adapter = new BindingRecyclerViewAdapter<SelectLanguageItemViewModel>() {
        @Override
        public void onBindBinding(ViewDataBinding binding, int variableId, int layoutRes, int position, final SelectLanguageItemViewModel item) {
            super.onBindBinding(binding, variableId, layoutRes, position, item);
            final HomeSelectLanguageItemBinding myBinding = (HomeSelectLanguageItemBinding) binding;
            if (StringUtils.equals(item.mItemEntity.get(), selectLanguage.get())) {
                oldItem = item;
                oldBinding = myBinding;
                item.isSelect.set(View.VISIBLE);
                myBinding.language.setSelected(true);
            }
            binding.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (oldItem == null)
                        oldItem = item;
                    else {
                        oldItem.isSelect.set(View.GONE);
                        oldBinding.language.setSelected(false);
                    }
                    item.isSelect.set(View.VISIBLE);
                    myBinding.language.setSelected(true);
                    selectLanguage.set(item.mItemEntity.get());
                    oldBinding = myBinding;
                    oldItem = item;
                }
            });
        }
    };
}
